package cn.tedu.tea.service;

import cn.tedu.tea.domain.ContentDetail;
import cn.tedu.tea.domain.param.ContentDetailAddNewDTO;
import cn.tedu.tea.domain.param.ContentDetailUpdateDTO;
import cn.tedu.tea.domain.vo.ContentDetailStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.ContentDetailMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ContentDetailServiceImpl implements IContentDetailService {

    @Autowired
    private ContentDetailMapper contentDetailMapper;

    @Override
    public void addNew(ContentDetailAddNewDTO contentDetailAddNewDTO) {
        ContentDetail contentDetail = new ContentDetail();
        BeanUtils.copyProperties(contentDetailAddNewDTO, contentDetail);
        int rows = contentDetailMapper.insert(contentDetail);
        if (rows != 1){
            String message = "添加内容失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        int rows = contentDetailMapper.deleteById(id);
        if (rows != 1){
            String message = "删除内容失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void update(ContentDetailUpdateDTO contentDetailUpdateDTO) {
        ContentDetail contentDetail = new ContentDetail();
        BeanUtils.copyProperties(contentDetailUpdateDTO, contentDetail);
        int rows = contentDetailMapper.update(contentDetail);
        if (rows != 1){
            String message = "修改内容失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public ContentDetailStandardVO getStandardById(Integer id) {
        ContentDetailStandardVO contentDetailStandard = contentDetailMapper.getStandardById(id);
        if (contentDetailStandard == null){
            String message = "查询内容详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return contentDetailStandard;
    }

}
